package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.HomePageListBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.DateUtils;
import com.gigamole.infinitecycleviewpager.HorizontalInfiniteCycleViewPager;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/4/16.
 */

public class NewsHomeAdapter2 extends BaseQuickAdapter<HomePageListBean.ResultBean.ListBean, BaseViewHolder> {
    private Context context;
    private boolean isTop;

    public NewsHomeAdapter2(@Nullable List<HomePageListBean.ResultBean.ListBean> data, Context context, boolean isTop) {
        super(data);
        this.context = context;
        this.isTop = isTop;
        setMultiTypeDelegate(new MultiTypeDelegate<HomePageListBean.ResultBean.ListBean>() {
            @Override
            protected int getItemType(HomePageListBean.ResultBean.ListBean entity) {
                return entity.getListlayout();
            }
        });
        getMultiTypeDelegate()
                .registerItemType(2, R.layout.layout_newstext)
                .registerItemType(-2, R.layout.layout_adrolllayout)
                .registerItemType(-1, R.layout.layout_adlayout)
                .registerItemType(0, R.layout.layout_newstext)
                .registerItemType(1, R.layout.layout_newsimage);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final HomePageListBean.ResultBean.ListBean item) {

        BitmapUtils bitmapUtils = new BitmapUtils(context);
        switch (helper.getItemViewType()) {
            case 0:
                helper.setText(R.id.tv_newstext_title, item.getTitle());
                helper.setText(R.id.tv_newstext_date, DateUtils.getTimeOfYMD(item.getAdd_time()));

                if (isTop) {
                    if (item.getIstoutiao() == 1) {

                        helper.getView(R.id.tv_newstext_topnews).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_newstext_views).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.tv_newstext_topnews).setVisibility(View.GONE);
                        helper.getView(R.id.tv_newstext_views).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_newstext_views, "阅读 " + item.getView());
                    }
                } else {
                    helper.getView(R.id.tv_newstext_topnews).setVisibility(View.GONE);
                    helper.getView(R.id.tv_newstext_views).setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_newstext_views, "阅读 " + item.getView());
                }

                bitmapUtils.display(helper.getView(R.id.iv_newstext_icon), GenesisApiConfig.PIC_HOST + item.getCover_img());
                Glide.with(context).load(GenesisApiConfig.PIC_HOST + item.getCover_img()).into((ImageView) helper.getView(R.id.iv_newstext_icon));

                break;
            case 1:
                helper.setText(R.id.tv_newsimage_title, item.getTitle());
                Glide.with(context).load(GenesisApiConfig.PIC_HOST + item.getCover_img()).into((ImageView) helper.getView(R.id.iv_newsimage_icon1));
                Glide.with(context).load(GenesisApiConfig.PIC_HOST + item.getCover_img2()).into((ImageView) helper.getView(R.id.iv_newsimage_icon2));
                Glide.with(context).load(GenesisApiConfig.PIC_HOST + item.getCover_img3()).into((ImageView) helper.getView(R.id.iv_newsimage_icon3));
                helper.setText(R.id.tv_newsimage_date, DateUtils.getTimeOfYMD(item.getAdd_time()));

                if (isTop) {
                    if (item.getIstoutiao() == 1) {

                        helper.getView(R.id.tv_newsimage_topnews).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_newsimage_views).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.tv_newsimage_topnews).setVisibility(View.GONE);
                        helper.getView(R.id.tv_newsimage_views).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_newsimage_views, "阅读 " + item.getView());
                    }
                } else {
                    helper.getView(R.id.tv_newsimage_topnews).setVisibility(View.GONE);
                    helper.getView(R.id.tv_newsimage_views).setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_newsimage_views, "阅读 " + item.getView());
                }

                break;
            case -1:
                bitmapUtils.display(helper.getView(R.id.iv_adlayout), GenesisApiConfig.PIC_HOST + item.getCover_img());
                break;
            case -2:
                HorizontalInfiniteCycleViewPager horizontalInfiniteCycleViewPager = helper.getView(R.id.hor_pager);
                HorizontalPagerAdapter coverFlowAdapter = new HorizontalPagerAdapter(context, item.getAdvert_list());
                horizontalInfiniteCycleViewPager.setAdapter(coverFlowAdapter);

                break;
            case 2:
                helper.setText(R.id.tv_newstext_title, item.getTitle());
                helper.setText(R.id.tv_newstext_date, DateUtils.getTimeOfYMD(item.getAdd_time()));

                if (isTop) {
                    if (item.getIstoutiao() == 1) {

                        helper.getView(R.id.tv_newstext_topnews).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_newstext_views).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.tv_newstext_topnews).setVisibility(View.GONE);
                        helper.getView(R.id.tv_newstext_views).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_newstext_views, "阅读 " + item.getView());
                    }
                } else {
                    helper.getView(R.id.tv_newstext_topnews).setVisibility(View.GONE);
                    helper.getView(R.id.tv_newstext_views).setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_newstext_views, "阅读 " + item.getView());
                }

                helper.getView(R.id.iv_newstext_icon).setVisibility(View.GONE);

                break;

        }


    }
}
